package com.rwaste.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.rwaste.commonutils.ResultCode;
import com.rwaste.exceptionhandler.RwasteException;
import com.rwaste.vod.service.VodService;
import com.rwaste.vod.utils.ConstanVodUtils;
import com.rwaste.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    /**
     * 上传视频到阿里云
     *
     * @param file
     **/
    @Override
    public String uploadVideoAly(MultipartFile file) {
        UploadStreamRequest request = null;
        //title 上传之后显示的名称
        //fileName 上传文件原始名称
        //inputStream 上传文件输入流
        try{
            request = new UploadStreamRequest(ConstanVodUtils.ACCESS_KEY_ID,
                    ConstanVodUtils.ACCESS_KEY_SECRET,
                    file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")),
                    file.getOriginalFilename(),
                    file.getInputStream());
        }catch (Exception e){
            e.printStackTrace();
        }

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);

        String videoId = null;
        if (response.isSuccess()) {
            videoId = response.getVideoId();
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            videoId = response.getVideoId();
        }
        return videoId;
    }

    /**
     * 根据视频id删除阿里云视频
     *
     * @param id
     **/
    @Override
    public void removeVideoAly(String id) {
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodUtils.ACCESS_KEY_ID, ConstanVodUtils.ACCESS_KEY_SECRET);

            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //向request设置视频的id
            request.setVideoIds(id);

            //初始化对象的方法实现删除
            client.getAcsResponse(request);

        }catch (Exception e){
            e.printStackTrace();
            throw new RwasteException(ResultCode.ERROR, "删除视频失败");
        }
    }

    /**
     * 根据视频id删除多个阿里云视频
     *
     * @param videoIdList
     **/
    @Override
    public void removeMoreVideoAly(List<String> videoIdList) {
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodUtils.ACCESS_KEY_ID, ConstanVodUtils.ACCESS_KEY_SECRET);

            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList值转换为1, 2, 3
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");

            //向request设置视频的id
            request.setVideoIds(videoIds);

            //初始化对象的方法实现删除
            client.getAcsResponse(request);

        }catch (Exception e){
            e.printStackTrace();
            throw new RwasteException(ResultCode.ERROR, "删除视频失败");
        }
    }
}
